package laf.ps;

import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Scanner;
import java.util.TimeZone;

import laf.env.EnvObject;
import laf.env.ObjectIdentifier;

public class ProcessMonitor extends ObjectIdentifier {
	
	private static final String command = "D:\\Apps\\PsTools\\PsList.exe";
	
	public ProcessMonitor(String process) {
		super(process);
	}
	
	public void update() {
		Runtime runtime = Runtime.getRuntime();
		try {
			String[] cmd = {command, id };
			Process proc = runtime.exec(cmd);
			//proc.waitFor();
			InputStream is = proc.getInputStream();
			Scanner scanner = new Scanner(is);
			scanner.findWithinHorizon("Elapsed Time", 0);
			
			System.out.print("name:\t");
			System.out.println(scanner.next());
			
			System.out.print("pid:\t");
			System.out.println(scanner.next());
			
			System.out.print("pri:\t");
			int pri = scanner.nextInt();
			System.out.println(pri);
			
			System.out.print("thds:\t");
			int thds = scanner.nextInt();
			System.out.println(thds);
			
			int handles = scanner.nextInt();
			
			System.out.print("mem:\t");
			int mem = scanner.nextInt() / 1024;
			System.out.println(mem);
			
			System.out.print("cpu:\t");
			DateFormat df = DateFormat.getTimeInstance();
			df.setTimeZone(TimeZone.getTimeZone("GMT"));
			Date cpuTime = df.parse(scanner.next());
			long cpu = (int) cpuTime.getTime() / (60 * 60 * 10);
			//int cpu = scanner.nextInt();
			System.out.println(cpu);
			System.out.println(cpuTime);
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		ProcessMonitor pm = new ProcessMonitor("0");
		pm.update();
	}

}
